<template>
	<div>
		<el-dialog :title="storedFrom.title"  destroy-on-close v-model="storedFrom.open" width="400px" append-to-body close-on-click-modal="false">
			<el-form ref="storedFromRef" :model="storedFrom.form" :rules="storedFrom.rules" label-width="80px">
				<el-form-item label="储值余额" prop="storedAmount">
				   <el-input v-model="storedFrom.form.storedAmount" disabled/>
				</el-form-item>
				<el-form-item label="交易类型" prop="type">
				  <el-select v-model="storedFrom.form.type" placeholder="请选择交易类型" disabled>
					<el-option v-for="dict in bus_stored_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
				  </el-select>
				</el-form-item>
				<el-form-item label="交易金额" prop="transAmount">
				   <el-input type="number" v-model="storedFrom.form.transAmount"/>
				</el-form-item>
				<el-form-item label="交易备注" prop="remark" v-if="storedFrom.form.type=='2'">
				   <el-input v-model="storedFrom.form.remark" type="textarea"/>
				</el-form-item>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="saveStoredValue">确 定</el-button>
					<el-button @click="storedFrom.open=false">取 消</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup="diffStoredAmount">
	import {addStoredRecord} from "@/api/crm/storedRecord"
	const {
		proxy
	} = getCurrentInstance();
	const {
		bus_stored_type
	} = proxy.useDict('bus_stored_type');
	
	const emit = defineEmits(['success'])
	
	const storedFrom = ref({
		title:'',
		open:false,
		form:{
			
		},
		rules:{
			type: [{
				required: true,
				message: '请选择交易类型',
				trigger: 'change'
			}],
			transAmount: [{
				required: true,
				message: '请输入交易金额',
				trigger: 'change'
			},{ validator: function(r,v,callback){
				if(v==null||v==undefined||v==''){
					callback(new Error('请输入交易金额'))
				}else{
					if(storedFrom.value.form.type=='2'&&storedFrom.value.form.storedAmount<parseFloat(v)){
						callback(new Error('储值余额不够'))
					}else{
						callback()
					}
				}
			}, trigger: 'blur' }],
			remark: [{
				required: true,
				message: '请输入交易备注',
				trigger: 'blur'
			}]
		}
	})
	
	
	
	function saveStoredValue(){
			
			const _nickname = storedFrom.value.form.nickname||'用户';
			const _tipTitle = storedFrom.value.form.type=='1'?'储值':'消费'
			const _transAmount = storedFrom.value.form.transAmount
			
			proxy.$refs["storedFromRef"].validate(valid => {
				if (valid) {
					
					proxy.$modal.confirm('是否向'+_nickname +_tipTitle+ _transAmount + '元？').then(function() {
						return addStoredRecord(storedFrom.value.form);
					}).then((response) => {
						storedFrom.value.open = false;
						
						storedFrom.value.form.nickname = null;
						storedFrom.value.form.meId = null;
						storedFrom.value.form.storedAmount = null;
						storedFrom.value.form.type = null;
						storedFrom.value.form.transAmount = null;
						storedFrom.value.form.remark = null;
						
						proxy.$modal.msgSuccess(_tipTitle+"成功");
						
						emit('success',response.data);
						
					}).catch(() => {});
				}
			})
	}
		
	function openWin(meInfo,type){
		const nnn = meInfo.name||'客户'
		storedFrom.value.title = '1'==type?(nnn+'-储值'):(nnn+'-消费');
		storedFrom.value.open= true;
		
		
		storedFrom.value.form.nickname = meInfo.name;
		storedFrom.value.form.meId = meInfo.meId;
		storedFrom.value.form.storedAmount = meInfo.storedAmount;
		storedFrom.value.form.type = type;
	}
	
	
	defineExpose({
		openWin
	})
	
</script>

<style>
</style>